package iconcurrent;

/**
 * @author or2
 * @Description Object类 wait 与 notify 部分探究 - wait仅释放当前调用它的锁!!
 * @create 2021年09月10日 时间: 19:48
 */
public class WaitAndNotify {

    private static final Object LOCK_A = new Object();
    private static final Object LOCK_B = new Object();

    public static void main(String[] args) throws InterruptedException {
//——————————————————————————————————————————————————————————————————————————————————————————————————————
        Runnable taskA = () -> {
            synchronized (LOCK_A) {
                System.out.println(Thread.currentThread().getName() + "获取到 锁A!");
                synchronized (LOCK_B) {
                    System.out.println(Thread.currentThread().getName() + "获取到 锁B!");
                    try {
                        System.out.println(Thread.currentThread().getName() + "释放了 锁A!");
                        LOCK_A.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "退出 锁B互斥区。");
            }
            System.out.println(Thread.currentThread().getName() + "退出 锁A互斥区。");
        };
        Thread threadA = new Thread(taskA, "A");
        Thread threadB = new Thread(taskA, "B");
        threadA.start();
        threadB.start();
//——————————————————————————————————————————————————————————————————————————————————————————————————————
        Runnable taskB = () -> {
            synchronized (LOCK_B) {
                System.out.println(Thread.currentThread().getName() + "获取到 锁B!");
                synchronized (LOCK_A) {
                    System.out.println(Thread.currentThread().getName() + "获取到 锁A!");
                }
                System.out.println(Thread.currentThread().getName() + "退出 锁A互斥区。");
            }
            System.out.println(Thread.currentThread().getName() + "退出 锁B互斥区。");
        };
        Thread threadC = new Thread(taskB, "C");
//——————————————————————————————————————————————————————————————————————————————————————————————————————
    }
}
